package com.slz3.business.module.configManagement.conZoneManage.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conZoneType.domain.ConZoneType;
import com.slz3.business.module.configManagement.conZoneType.steward.ConZoneTypeSpecification;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class ConZoneManageFactory {

    /**
     * 初始化基础属性值
     */
    public static ConZoneManage initBaseFields(ConZoneManage conZoneManage) {
        if (conZoneManage.getUuid() == null) {
            conZoneManage.setUuid(UUIDUtils.generateUUID());
            conZoneManage.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(conZoneManage.getSerAudit())) {
                conZoneManage.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(conZoneManage.getSerUsing())) {
                conZoneManage.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            conZoneManage.setLastModifyTime(LocalDateTime.now());
        }
        return conZoneManage;
    }

    /**
     * 初始化基础属性值
     */
    public static ConZoneManage initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static ConZoneManage newTemStyle() {
        return new ConZoneManage();
    }

    public static ConZoneManageCondition newConZoneManageCondition(MultiValueMap<String, String> params) {

        ConZoneManageCondition condition = new ConZoneManageCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");
        String fkZoneTemplate = params.getFirst("fkZoneTemplate");
        String fkZoneType = params.getFirst("fkZoneType");

        if(VE.isNotEmpty(fkZoneTemplate) && VE.isEmpty(fkZoneType)){
            List<ConZoneType> conZoneTypeList = Repositorys.conZoneType.findAll(ConZoneTypeSpecification.findJoin(params));
            List<String> conZoneTypeIds = new ArrayList<>(conZoneTypeList.size());
            conZoneTypeList.forEach(conZoneType -> conZoneTypeIds.add(conZoneType.getUuid().toString()));
            fkZoneType = String.join(",", conZoneTypeIds);
        }


        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, ConZoneManageCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, ConZoneManageCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, ConZoneManageCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, ConZoneManageCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));
        condition.setFkZoneTypeIds(VE.isEmpty(fkZoneType) ? null : Arrays.stream(fkZoneType.split(",")).collect(Collectors.toList()));

        return condition;

    }

}
